/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.InvoiceDAO;
import com.ivanbiz.dao.impl.InvoiceDAOImpl;
import com.ivanbiz.model.Invoice;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Window;

/**
 *
 * @author IW20149X
 */
public class TagihanLaporanService extends SelectorComposer<Component> {

    @Wire
    Listbox listTagihan;
    InvoiceDAO invoiceDAO;
    SimpleDateFormat sdf;
    NumberFormat numberFormat;

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);
        invoiceDAO = new InvoiceDAOImpl();
        sdf = new SimpleDateFormat("dd MMM yyyy");
        numberFormat = NumberFormat.getNumberInstance();
        loadTagihan();

    }

    private void loadTagihan() {
        try {
            listTagihan.getItems().clear();
            int no = 0;
            int x = 0;
            List<Invoice> loadInvoice = invoiceDAO.getDataByEquals(Invoice.class, "status", "0");
            for (Invoice invoice : loadInvoice) {
                no += 1;
                String[] kepada;
                kepada = invoice.getDeskripsiKepada().split("#");
                String[] deskripsi;
                deskripsi = invoice.getDeskripsiUntuk().split("#");
                Listitem item = new Listitem();
                item.setValue(invoice);
                item.appendChild(new Listcell(String.valueOf(no)));
                item.appendChild(new Listcell(sdf.format(invoice.getDate())));
                item.appendChild(new Listcell(invoice.getNII()));
                item.appendChild(new Listcell(invoice.getKelas().getTransactionReference()));
                item.appendChild(new Listcell(invoice.getBank().getNama()));
                item.appendChild(new Listcell(kepada[1] + " A/C No. " + kepada[0]));
                item.appendChild(new Listcell(invoice.getDeskripsiUntukPembayaran()));
                item.appendChild(new Listcell(invoice.getDeskripsiJumlahPeserta()));
                item.appendChild(new Listcell("Rp " + numberFormat.format(invoice.getJumlahTagihan()) + ",00"));
                item.appendChild(new Listcell(invoice.getJatuhTempo()));
                item.appendChild(new Listcell(deskripsi[1] + " A/C No. " + deskripsi[0]));
                item.appendChild(new Listcell(invoice.getStatus().equals("1") ? "N" : "Y"));
                listTagihan.appendChild(item);
                ++x;
            }
        } catch (Exception ex) {
            Logger.getLogger(TagihanLaporanService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Listen("onClick=#lihat;")
    public void lihat() {
        Invoice invoice = listTagihan.getSelectedItem().getValue();
        Map map = new HashMap();
        map.put("invoice", invoice);
        Window win = (Window) Executions.createComponents("/tagihanLaporanView.zul", null, map);
        win.doModal();
    }
}
